https://www.javatpoint.com/different-ways-of-reading-a-text-file-in-java Different Ways of Reading a Text File in Java In Java, there are various methods for creating and accessing text files. It is necessary to do this when working with numerous apps. Java has multiple methods for reading plain text files, such as FileReader, BufferedReader, and Scanner. Each utility offers a unique feature; for example, BufferedReader buffers data for quick reading, whereas Scanner has parsing capabilities. There are different approaches for reading of text file in java. They are as follows. BufferedReader class for Reading text file FileReader class for Reading text file Scanner class for reading text file Reading the whole file in a List Read a text file as String In Java, we can also read a text file line by line by using Scanner and BufferReader. Next, java.util.stream.Stream, a new Stream class introduced in Java SE 8, offers a more effective and slow method of reading a file. Approach: BufferedReader class for Reading text file Text is read using this method from a character input stream. It does have a buffer to make reading characters, arrays, and lines more efficient. One can either utilise the default size of the buffer or specify the size. For most uses, the default value is sufficient. Typically, every read request issued to a reader prompts an equivalent read request to be issued to the character or byte stream underneath it. Backward Skip 10s Play Video Forward Skip 10s Therefore, as seen below, it is recommended to wrap any Reader, including FileReaders and InputStreamReaders, whose read() operations may be expensive, in a BufferedReader: Syntax: BufferedReader input = new BufferedReader(Reader input, int size); Implementation: FileName: ReadingTextFile.java import java.io.*; import java.util.*; public class ReadingTextFile { public static void main(String[] args) throws Exception { // File path is supplied as an argument. File file = new File("C:\\Users\\vaish\\Documents\\test.txt"); // Creating an object of the BufferedReader class BufferedReader input = new BufferedReader(new FileReader(file)); String str; // The condition is met until a character appears in the string. while ((str = input.readLine()) != null) // Print the string present in file System.out.println(str); } } Output: Hello World Welcome to the house Have a wonderful day Approach: FileReader class for Reading text file A realistic course on reading character files. The constructors of this class operate under the assumption that the default byte-buffer size and character encoding are suitable. The following constructors are defined in this class: Advertisement FileReader(File file): Provide the file to read from when creating a new FileReader. FileReader(FileDescriptor fd): Provide the FileDescriptor to read from when creating a new FileReader. FileReader(String fileName): Gives the name of the file to be read from when creating a new FileReader. Implementation: FileName: ReadingTextFile1.java import java.util.*; import java.io.*; public class ReadingTextFile1 { public static void main(String[] args) throws Exception { // Sending the file's path as an argument FileReader input = new FileReader("C:\\Users\\vaish\\Documents\\test.txt"); int i; // holds true until there's nothing left to write in file while ((i = input.read()) != -1) System.out.print((char)i); } } Output: Hello World Welcome to the house Have a wonderful day Approach: Scanner class for reading text file A straightforward text scanner that uses regular expressions to interpret strings and primitive types. By matching whitespace as the default delimiter pattern, a scanner divides its input into tokens. The many subsequent methods can then be used to transform the generated tokens into values of various sorts. Case 1: With Using loops Implementation: FileName: ReadingTextFile2.java import java.io.File; import java.util.Scanner; public class ReadingTextFile2 { public static void main(String[] args) throws Exception { // pass the path to the file as a parameter File file = new File("C:\\Users\\vaish\\Documents\\test.txt"); Scanner scn = new Scanner(file); while (scn.hasNextLine()) System.out.println(scn.nextLine()); } } Output: Hello World Welcome to the house Have a wonderful day Case 2: Without Using loops Implementation: Advertisement FileName: ReadingTextFile3.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingTextFile3 { public static void main(String[] args) throws FileNotFoundException { File file = new File("C:\\Users\\vaish\\Documents\\test.txt"); Scanner scn = new Scanner(file); // To delimit, all we have to do is utilise \\Z. scn.useDelimiter("\\Z"); System.out.println(scn.next()); } } Output: Hello World Welcome to the house Have a wonderful day Approach: Reading the whole file in a List Open a file and read every line. By using this method, you may be guaranteed that the file will close either when all the bytes have been read or if a runtime exception or I/O fault is raised. The chosen charset is used to decode the file's bytes into characters. Advertisement Syntax: public static List readAllLines(Path path,Charset cs)throws IOException Note: Advertisement The following are recognised as line terminators by the above method: \u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED \u000A, LINE FEED \u000D, CARRIAGE RETURN Implementation: FileName: ReadingTextFile4.java import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; public class ReadingTextFile4 { public static ListreadFileInList(String file) { List lines = Collections.emptyList(); try { lines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } return lines; } public static void main(String[] args) { List l = readFileInList("C:\\Users\\vaish\\Documents\\test.txt"); Iterator tem = l.iterator(); while (tem.hasNext()) System.out.println(tem.next()); } } Output: Hello World Welcome to the house Have a wonderful day Approach: Read a text file as String Java's File Class's readString() function is used to read content from the provided file. Syntax: Files.readString(filePath) ; Return Value: The file's content is returned by this method in String format. Note: Since Java 11 was released, files have been read into String using the File.readString() method. Implementation: Advertisement FileName: ReadingTextFile5.java import java.io.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; public class ReadingTextFile5 { public static void main(String[] args) throws IOException { // Building an object of the Path class to create a path // choosing file from the local directory Path file = Path.of("C:\\Users\\vaish\\Documents\\test.txt"); // Calling Files now.use the readString() function to read the file String str = Files.readString(file); System.out.println(str); } } Output: Hello World Welcome to the house Have a wonderful day